import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';

void main() => runApp(TestPage());

class TestPage extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'test',
        theme: ThemeData(
            primaryColor: Colors.cyan,
            brightness: Brightness.light,
            accentColor: Colors.black),
        home: Demo());
  }
}

class Demo extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(appBar: AppBar(title: Text('头')), body: Body());
  }
}

class Body extends StatefulWidget {
  @override
  _BodyState createState() => _BodyState();
}

class _BodyState extends State<Body> {
////  void hh(event) {
////    print('hh down $event');
////  }
  double _top = 0.0;
  double _left = 0.0;

  @override
  Widget build(BuildContext context) {
//    return Listener(
//      child: Container(
//        color: Colors.yellow,
//        width: 300,
//        height: 300,
//      ),
//      onPointerDown: hh,
//      onPointerMove: (event) => print("move $event"),
//      onPointerUp: (event) => print("up $event"),
//    );

//    return Stack(children: <Widget>[
//      Positioned(
//        top: _top,
//        left: _left,
//        child: GestureDetector(
//          child: Container(color: Colors.red, width: 50, height: 50),
//          onTap: () => print("Tap"),
//          onDoubleTap: () => print('Double Tap'),
//          onLongPress: () => print('Long Press'),
//          onPanUpdate: (e) {
//            setState(() {
//              _left += e.delta.dx;
//              _top += e.delta.dy;
//            });
//          },
//        ),
//      )
//    ]);

      return RawGestureDetector(//自己构造父Widget的手势识别映射关系
          gestures: {
            //建立多手势识别器与手势识别工厂类的映射关系，从而返回可以响应该手势的recognizer
            MultipleTapGestureRecognizer: GestureRecognizerFactoryWithHandlers<
                MultipleTapGestureRecognizer>(
                  () => MultipleTapGestureRecognizer(),
                  (MultipleTapGestureRecognizer instance) {
                instance.onTap = () => print('parent tapped');//点击回调
              },
            )
          },
          child: Container(
            color: Colors.pinkAccent,
            child: Center(
              child: GestureDetector(
                onTap: () => print('child tapped'),
                child: Container(
                  color: Colors.blueAccent,
                  width: 200.0,
                  height: 200.0,
                ),
              ),
          ),
        )
      );

      return GestureDetector(
        onTap: () => print('Parent tapped'),
        child: Container(
          color: Colors.pinkAccent,
          child: Center(
            child: GestureDetector(
              onTap: () => print('child tapped'),
              child: Container(
                color: Colors.blueAccent,
                width: 200.0,
                height: 200.0,
              ),
            )
          )
        )
      );
  }
}

class MultipleTapGestureRecognizer extends TapGestureRecognizer {
  @override
  void rejectGesture(int pointer) {
    acceptGesture(pointer);
  }
}